<?php

class Admin_SeoController extends Zend_Controller_Action
{

	public function init()
	{
		$translate  = Zend_Registry::get('Zend_Translate');
		$this->view->breadCrumbs = $this->view->crumbs();
		$this->view->breadCrumbs->addPrepend('/', $translate->_('Home'))
		->setMvc(true);

		$this->view->crumbs = $this->view->breadCrumbs->init();
		/* Initialize action controller here */
	}

	public function indexAction()
	{
		$this->_redirect($this->_request->getParam('module').'/'. $this->_request->getParam('controller') .'/list');
	}

	/////////////////////////////////////////////////////////////////////////////////

	public function addAction()
	{

		$seoFacade = new Admin_Model_Facade_Seo();
		$translate = Zend_Registry::get('Zend_Translate');
		$ToSeo = new Admin_Model_TO_Seo();
		$s_Sass = new Zend_Session_Namespace('s_Sass');
		$request  = $this->getRequest();
		$idUser = $s_Sass->UserId;
		$company = $s_Sass->CompanyId;
		$request  = $this->getRequest();
		$x_id  = $this->_request->getParam('Id');


		$form = new Admin_Form_Seo();
		$form->setAction(Zend_Controller_Front::getInstance()->getBaseUrl() . DIAG_PATH .
		$this->_request->getParam('module').DIAG_PATH.$this->_request->getParam('controller').DIAG_PATH.'add');

		if($request->isPost())
		{

			$data= $this->_request->getPost();

			$ToSeo->setId($data['Id']);
			$ToSeo->setTitle($data['Title']);
			$ToSeo->setKeywords($data['Keywords']);
			$ToSeo->setDescription($data['Description']);
			$ToSeo->setDistribution($data['Distribution']);
			$ToSeo->setSubject($data['Subject']);
			$ToSeo->setCity($data['City']);
			$ToSeo->setCountry($data['Country']);
			$ToSeo->setLanguage($data['Language']);
			$ToSeo->setRevisit($data['Revisit']);
			$ToSeo->setRobots($data['Robots']);
			$ToSeo->setModifiedBy(isset($idUser) ? $idUser : 1);

			if($ToSeo->getId()>0){
				 
				$data = $seoFacade->updateSeo($ToSeo->getAllVars());
				if($data)
				$s_Sass->messageError = 'Seo Actualizado';
			}
			$this->_redirect($this->_request->getParam('module').DIAG_PATH.'company/');

		}

		if($x_id>0 && is_numeric($x_id)){


			$ToSeo->setId($x_id);
			$data=$seoFacade->readSeo($ToSeo->getAllVars());

			$form = new Admin_Form_Seo($data);
			$this->view->module= $this->_request->getParam('module');
			$this->view->controller= $this->_request->getParam('controller');
			$form->Id->setValue($x_id);
			$this->view->form=$form;
			$this->view->Data=$data;

		}
		else
		{
			$form = new Admin_Form_Seo();
			$form->setAction(
			Zend_Controller_Front::getInstance()
			->getBaseUrl() . DIAG_PATH .
			$this->_request->getParam('module').DIAG_PATH.
			$this->_request->getParam('controller').DIAG_PATH.'add'
			);
			$this->view->module= $this->_request->getParam('module');
			$this->view->controller= $this->_request->getParam('controller');

			//////////////////////////
			$this->view->form=$form;

		}
	}

	///////////////////////////////////////////////
	public function listAction()
	{

		$s_Sass = new Zend_Session_Namespace('s_Sass');
		$FacadeSeo = new Admin_Model_Facade_Seo();
		$company=$s_Sass->CompanyId;
		$request  = $this->getRequest();

		if (strlen($s_Sass->message)>0)
		{
			$this->view->message = $s_Sass->message;
			$s_Sass->message="";
		}

		$data = $FacadeSeo->readAllSeo();
		$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Array($data));
		$paginator	->setItemCountPerPage(12)
		->setCurrentPageNumber($this->_getParam('page', 1));

		$this->view->paginator = $paginator;
		$this->view->module= $this->_request->getParam('module');
		$this->view->controller= $this->_request->getParam('controller');
	}

	//////////////////////////////////////////////////////////
	public function deleteAction()
	{
		$seoFacade=	new Admin_Model_Facade_Seo();
		$ToSeo=		new Admin_Model_TO_Seo();
		$translate = 	Zend_Registry::get('Zend_Translate');
		$s_Sass = 	new Zend_Session_Namespace('s_Sass');

		$request=$this->getRequest();
		$id=$this->_request->getParam('Id');
		$modifico =($s_Sass->UserId != 0)? $s_Sass->UserId : 1;

		$ToSeo->setId($id);
		$ToSeo->setModifiedBy($modifico);

		$data=$seoFacade->deleteSeo($ToSeo);
		if($data)
		$s_Sass->message = $translate->_('SEO deleted.');

		$this->_redirect($this->_request->getParam('module').DIAG_PATH.$this->_request->getParam('controller').DIAG_PATH.'list');
	}
}


?>
